/*
 * ConnexionGui.java
 *
 * Created on 30 mai 2007, 10:37
 */

package bibliotheque.client.gui;

import bibliotheque.client.Client;
import bibliotheque.events.ConnexionOccuredListener;
import bibliotheque.events.ConnexionOccuredEvent;
import bibliotheque.events.ConnexionQuitListener;
import bibliotheque.events.ConnexionQuitOccuredEvent;
import bibliotheque.nameserver.remote.NameServerRemoteIS;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

/**
 *
 * @author  guillaume
 */
public class ConnexionGui extends javax.swing.JPanel {
    
    private String service;
    private Registry registry;
    
    private ArrayList<ConnexionOccuredListener> connexion_listener;
    private ArrayList<ConnexionQuitListener> connexionQuitListener;
    
    public void AddConnexionQuitListener(ConnexionQuitListener listener) {
        this.connexionQuitListener.add(listener);
    }
    
    public void RemoveConnexionQuitListener(ConnexionQuitListener listener) {
        this.connexionQuitListener.remove(listener);
    }
    
    protected void ConnexionQuitOccured(){
        ConnexionQuitOccuredEvent event = new ConnexionQuitOccuredEvent(this);
        for(int i=0;i<this.connexionQuitListener.size();i++) {
            this.connexionQuitListener.get(i).ConnexionQuitOccured(event);
        }
    }
    
    public void AddConnexionListener(ConnexionOccuredListener listener) {
        this.connexion_listener.add(listener);
    }
    
    public void RemoveConnexionListener(ConnexionOccuredListener listener) {
        this.connexion_listener.remove(listener);
    }
    
    protected void ConnexionOccured(String service, String adresseRmi, int portRmi){
        ConnexionOccuredEvent event = new ConnexionOccuredEvent(this, adresseRmi, portRmi, this.jComboBoxService.getSelectedItem().toString(), this.jTextFieldIp.getText());
        for(int i=0;i<this.connexion_listener.size();i++) {
            this.connexion_listener.get(i).ConnexionOccured(event);
        }
    }
    
    /** Creates new form ConnexionGui */
    public ConnexionGui() {
        initComponents();
        this.connexion_listener = new ArrayList<ConnexionOccuredListener>();
        this.connexionQuitListener = new ArrayList<ConnexionQuitListener>();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    
    public void initComponents() {
        jLabelService = new javax.swing.JLabel();
        jLabeladresse = new javax.swing.JLabel();
        jLabelPort = new javax.swing.JLabel();
        jTextFieldAdresse = new javax.swing.JTextField();
        jTextFieldPort = new javax.swing.JTextField();
        jButtonRefresh = new javax.swing.JButton();
        jButtonValidate = new javax.swing.JButton();
        jButtonStop = new javax.swing.JButton();
        jComboBoxService = new javax.swing.JComboBox();
        jLabelIp = new JLabel();
        jTextFieldIp = new JTextField();
        
        jLabelIp.setText("Adresse IP");
        
        jTextFieldIp.setSize(200,19);
        jTextFieldIp.setMaximumSize(new Dimension(200,19));
        jTextFieldIp.addCaretListener(new CaretListener() {
            public void caretUpdate(CaretEvent e) {
                jTextFieldIpCaretUpdate(e);
            }
        });
        
        jLabelService.setText("Service : ");
        
        jComboBoxService.setSize(new Dimension(300,24));
        jComboBoxService.setMaximumSize(new Dimension(300,24));
        
        jLabeladresse.setText("Adresse du rmiregistery :");
        
        jLabelPort.setText("Port du rmiregistery :");
        
        jTextFieldAdresse.setText("127.0.0.1");
        jTextFieldAdresse.setMaximumSize(new Dimension(200,19));
        
        jTextFieldPort.setText("2222");
        jTextFieldPort.setMaximumSize(new Dimension(200,19));
        
        jButtonRefresh.setText("Refresh");
        jButtonRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRefreshActionPerformed(evt);
            }
        });
        
        jButtonValidate.setText("Validate");
        jButtonValidate.setEnabled(false);
        jButtonValidate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonValidateActionPerformed(evt);
            }
        });
        
        jButtonStop.setText("Stop");
        jButtonStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonStopActionPerformed(evt);
            }
        });
        
        jComboBoxService.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxServiceActionPerformed(evt);
            }
        });
        
        Box hBox1 = Box.createHorizontalBox();
        hBox1.add(this.jLabelService);
        hBox1.add(Box.createHorizontalStrut(10));
        hBox1.add(this.jComboBoxService);
        hBox1.add(Box.createHorizontalStrut(10));
        hBox1.add(this.jButtonRefresh);
        
        Box hBox2 = Box.createHorizontalBox();
        hBox2.add(this.jLabeladresse);
        hBox2.add(Box.createHorizontalStrut(10));
        hBox2.add(this.jTextFieldAdresse);
        
        Box hBox3 = Box.createHorizontalBox();
        hBox3.add(this.jLabelPort);
        hBox3.add(Box.createHorizontalStrut(10));
        hBox3.add(this.jTextFieldPort);
        
        Box hBox4 = Box.createHorizontalBox();
        //hBox4.add(Box.createGlue());
        hBox4.add(this.jButtonStop);
        hBox4.add(Box.createHorizontalStrut(50));
        hBox4.add(this.jButtonValidate);
        //hBox4.add(Box.createGlue());
        
        Box hBox5 = Box.createHorizontalBox();
        hBox5.add(this.jLabelIp);
        hBox5.add(Box.createHorizontalStrut(50));
        hBox5.add(this.jTextFieldIp);
        
        Box vBox1 = Box.createVerticalBox();
        //vBox1.add(Box.createGlue());
        vBox1.add(hBox5);
        vBox1.add(Box.createVerticalStrut(50));
        vBox1.add(hBox1);
        vBox1.add(Box.createVerticalStrut(50));
        vBox1.add(hBox2);
        vBox1.add(Box.createVerticalStrut(50));
        vBox1.add(hBox3);
        vBox1.add(Box.createVerticalStrut(50));
        vBox1.add(hBox4);
        //vBox1.add(Box.createGlue());
        
        this.setLayout(new BorderLayout());
        this.add(vBox1,BorderLayout.CENTER);
    }
    
    
    private void jTextFieldIpCaretUpdate(CaretEvent event) {
        this.LocalStateChanged();
    }
    
    private void jComboBoxServiceActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
        this.LocalStateChanged();
    }
    
    private void jButtonRefreshActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
        try{
            this.jComboBoxService.removeAllItems();
            
            this.registry = LocateRegistry.getRegistry(this.jTextFieldAdresse.getText(),Integer.parseInt(this.jTextFieldPort.getText()));
            String[] listbound = this.registry.list();
            for(int i=0 ; i < listbound.length ; i++){
                if (registry.lookup(listbound[i]) instanceof NameServerRemoteIS) {
                    this.jComboBoxService.addItem(listbound[i]);
                }
            }
            
        } catch (RemoteException ex) {
            ex.printStackTrace();
        } catch (NotBoundException ex) {
            ex.printStackTrace();
        }
    }
    
    private void jButtonValidateActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
        
        if(this.jComboBoxService.getSelectedItem().equals("bibliotheque")){
            this.ConnexionOccured(this.jComboBoxService.getSelectedItem().toString(),this.jTextFieldAdresse.getText(),Integer.parseInt(this.jTextFieldPort.getText()));
        }
    }
    
    private void jButtonStopActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
        //this.dispose();
        this.ConnexionQuitOccured();
    }
    
    private void LocalStateChanged(){
        if(this.jTextFieldIp.getText().isEmpty() || this.jComboBoxService.getItemCount() == 0){
            this.jButtonValidate.setEnabled(false);
        } else {
            this.jButtonValidate.setEnabled(true);
        }
    }
    
    // Variables declaration - do not modify
    private javax.swing.JButton jButtonRefresh;
    private javax.swing.JButton jButtonStop;
    private javax.swing.JButton jButtonValidate;
    private javax.swing.JComboBox jComboBoxService;
    private javax.swing.JLabel jLabelPort;
    private javax.swing.JLabel jLabelService;
    private javax.swing.JLabel jLabeladresse;
    private javax.swing.JTextField jTextFieldAdresse;
    private javax.swing.JTextField jTextFieldPort;
    private JLabel jLabelIp;
    private JTextField jTextFieldIp;
    // End of variables declaration
    
}
